<template>
    <el-row class="lives-news">
        <el-col class="title">
            <h3>书法作品</h3>
        </el-col>
        <el-col>
            <el-form :inline="true">
                <el-form-item>
                    <el-button type="primary" @click="showAddDialog"><i class="el-icon-plus el-icon--left"></i>添加文件</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <el-col>
            <el-table :data="fileList">
                <el-table-column prop="pictureName" label="文件名称" align="center"  width="180px"/>
                <el-table-column label="预览图片" >
                    <template slot-scope="scope">
                        <a class="file_img" :href="'//uploads.gaoweizhou.com.cn/'+scope.row.pictureURL" :style="{backgroundImage: 'url(//uploads.gaoweizhou.com.cn/'+scope.row.pictureURL + ')'}" target="_blank">
                            111
                        </a>
                    </template>
                </el-table-column>
                <el-table-column label="文件地址" >
                    <template slot-scope="scope"><a :href="'//uploads.gaoweizhou.com.cn/'+scope.row.pictureURL" target="_blank">{{'//uploads.gaoweizhou.com.cn/'+scope.row.pictureURL}}</a></template>
                </el-table-column>
                <el-table-column label="操作" width="160px" align="center">
                    <template slot-scope="scope">
                        <el-button size="small" @click="updateVideoCategory(scope.$index, scope.row)">修改</el-button>
                        <el-button size="small" type="danger" @click="deleteVideoCategory(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-col>
        <el-dialog :title="isAdd?'新增文件':'修改文件'" :visible.sync="addDialog" size="tiny" @close="closeaddDialog" class="dialog">
            <el-form :model="formInline" label-width="120px">
                <el-form-item label="文件名称">
                    <el-input v-model="formInline.pictureName"></el-input>
                </el-form-item>
                <el-form-item label="文件">
                    <el-upload class="avatar-uploader" :headers="uploadHeader" :action="BASEURL + '/upload/file'" :show-file-list="false" :on-success="handleAvatarSuccess">
                        <el-button size="small" type="primary" :loading="uploadLoading">点击上传</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item label="文件地址">
                    <a :href="formInline.pictureURL" class="pictureURL" target="_blank">{{formInline.pictureURL}}</a>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelAddFile">取 消</el-button>
                <el-button type="primary" @click="addFile" :loading="addLoading">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 分页 -->
        <el-col class="pagination">
            <el-pagination @current-change="currentChange" :current-page="pageId" :page-size="pageSize" layout="total, prev, pager, next" :total="counts">
            </el-pagination>
        </el-col>
    </el-row>
</template>
<script>
import {
    getFileLists,
    addFile,
    deleteFileById,
    updateFileById,
    uploadFiles
} from '@/api/api'
export default {
    data: function() {
        return {
            addLoading: false,
            addDialog: false,
            formInline: {
                pictureId: 0,
                pictureName: null,
                pictureType: 'CALLIGRAPHY',
                pictureURL: null
            },
            pageId: 1,
            pageSize: 10,
            counts: 0,
            isAdd: true,
            uploadLoading: false,
            fileList: null,
            uploadHeader: {
                token: sessionStorage.getItem('jwt')
            },
            pictureId:0
        }
    },
    created: function() {
        this.pageId = parseInt(this.$route.query.page) || 1;
        this.getFileList();
    },
    methods: {
        //获取视频分类列表
        getFileList: function() {
            getFileLists({ params: { pageId: this.pageId, pageSize: this.pageSize, pictureType: 'CALLIGRAPHY'} }).then(data => {
                this.counts = data.count;
                this.fileList = data.list;
            })
        },
        //显示添加弹窗
        showAddDialog: function() {
            this.addDialog = true;
        },
        //关闭添加弹窗
        closeaddDialog: function() {
            this.isAdd = true;
            this.addLoading = false;
            this.formInline = {
                pictureId: 0,
                pictureName: null,
                pictureType: 'CALLIGRAPHY',
                pictureURL: null
            }
        },
        //添加文件
        addFile: function() {
            this.addLoading = true;
            if(!this.formInline.pictureName){
                this.$message({
                    message: '请填写文件名称',
                    type: 'error'
                })
                return;
            }
            if(!this.formInline.pictureURL){
                this.$message({
                    message: '请上传文件',
                    type: 'error'
                })
                return;
            }
            if (this.isAdd) {
                addFile(this.formInline).then(data => {
                    this.getFileList();
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    })
                    this.addDialog = false;
                })
            } else {
                updateFileById(this.formInline).then(data => {
                    this.getFileList();
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    })
                    this.addDialog = false;
                }).catch(err => {
                    console.error(err)
                })
            }
        },
        cancelAddFile: function() {
            this.addDialog = false;
        },
        updateVideoCategory: function(index, row) {
            this.isAdd = false;
            this.addDialog = true;
            this.formInline = {
                pictureId: row.pictureId,
                pictureName: row.pictureName,
                pictureType: row.pictureType,
                pictureURL: row.pictureURL
            }
        },
        deleteVideoCategory: function(index, row) {
            this.$confirm('是否删除该文件?', '删除文件', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteFileById(row.pictureId).then(() => {
                    this.getFileList();
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //分页
        currentChange: function(val) {
            this.$router.push('?page=' + val)
            this.pageId = val;
            this.getFileList()
        },
        handleAvatarSuccess: function(res){
            console.log(res)
            if(res.status){
                this.$message({
                    message: '上传文件成功',
                    type: 'success'
                })
                this.formInline.pictureURL = res.data.webPath;
            }else{
                this.$message({
                    message: '上传文件失败',
                    type: 'error'
                })
                this.formInline.pictureURL = null;
            }
        }
    }
}
</script>
<style scoped>
.file_img {
    height: 280px;
    width: 280px;
    display: block;
    color: rgba(0,0,0,0);
    background-size: contain;
    background-repeat: no-repeat;
    background-position: 50% 50%;
}
.lives-news {
    .title {
        font-size: 12px;
        border-bottom: 1px solid #eaeaea;
        margin-bottom: 5px;
        h3 {
            display: inline-block;
            padding: 5px 0;
            margin: 0;
            font-weight: normal;
            color: #23b7e5;
            border-bottom: 1px solid #23b7e5;
        }
    }
    .pictureURL{
        word-wrap: break-word;
    }
}
</style>